<?php
namespace Common\Model;
use Think\Model;
use Common\Model\VRAdminModel;
class AdminUserModel extends VRAdminModel
{
	
	protected $_validate = array(
	);
	
	//获取业务管理员列表
	function getUserAdminList($keyword = null, $pageStart, $pageSize){
		$resultData = array();
		$where = array();
		$where['status'] = array('EQ','1');
		$where['type'] = array('EQ','2');
		$where['real_name'] = array('like','%'.$keyword.'%');
		$count_Total = $this->where($where)->count();
		$area_list = $this->where($where)
		->order("admin_id asc")
		->limit($pageStart, $pageSize)
		->select();
		$resultData['data'] = $area_list;
		$resultData['count_Total'] = $count_Total;
		return $resultData;
	}
	
	//获取业务管理员栏目权限
	function getColumnRole($admin_id = null, $manage_personnel_id = 0){
		$column_role_arr = array();
		$vote_menu_id = M('vradmin1.sys_menu')->where(array('name'=>'投票'))->getField('menu_id');
		$show_count_menu_id = M('vradmin1.sys_menu')->where(array('name'=>'查看真实浏览数'))->getField('menu_id');
		$invite_reg_menu_id = M('vradmin1.sys_menu')->where(array('name'=>'邀请注册'))->getField('menu_id');
                $video_source_id = M('vradmin1.sys_menu')->where(array('name'=>'视频素材管理'))->getField('menu_id');
                $video_tag_id = M('vradmin1.sys_menu')->where(array('name'=>'视频标签管理'))->getField('menu_id');
		if(!$show_count_menu_id) $show_count_menu_id = '0';

		if($admin_id){
			//新闻业务权限列表
			$column_role_list = $this->alias("a")
			->join("vrnews1.news_column_admin b on a.admin_id = b.admin_id")
			->join("vrnews1.news_column c on b.column_id = c.column_id")
			->where('a.admin_id = '.$admin_id.' and c.status = 1')
			->field("b.column_id,c.name,c.type")
			->order("b.id desc")
			->select();
			
			//其他业务权限列表
			$sys_menu_model = M('vradmin1.menu','sys_');
			$sys_menu_role_list = $sys_menu_model->alias("a")
			->join("vradmin1.sys_menu_admin b on a.menu_id = b.menu_id")
			->where('b.admin_id = '.$admin_id." and b.menu_id in(166,286,306,$vote_menu_id, $manage_personnel_id, $show_count_menu_id,$invite_reg_menu_id,$video_source_id,$video_tag_id)")
			->field("a.menu_id,a.name,a.icon")
			->order("a.menu_id desc")
			->select();
			
			//地区列表
			$area_admin_model = M('vrnews1.admin','area_');
			$area_role_list = $area_admin_model->alias("a")
			->join("vrnews1.area b on a.area_id = b.area_id")
			->where('a.admin_id = '.$admin_id)
			->field("b.*")
			->order("a.id desc")
			->select();
			$menu_id = M('vradmin1.sys_menu')->where(array('name'=>'员工管理'))->getField('menu_id');
			//所属公司名称
            $company_info = M('vradmin1.sys_menu_admin')
                ->alias('s')
                ->field('c.name')
                ->join('RIGHT JOIN vrnews1.company c ON s.company_id = c.company_id')
                ->where(array('s.admin_id'=>$admin_id, 's.company_id'=>array('neq', 0), 's.menu_id'=>$menu_id))
                ->find();
		}else{
			//所有新闻业务列表
			$column_model = M('vrnews1.column','news_');
			$column_role_list = $column_model->where('status = 1')->field('column_id,name,type')->select();
			
			//所有其他业务列表
			$sys_menu_model = M('vradmin1.menu','sys_');
			$sys_menu_role_list = $sys_menu_model
				->where(" menu_id in(166,286,306,$vote_menu_id,$manage_personnel_id, $show_count_menu_id, $invite_reg_menu_id,$video_source_id,$video_tag_id)")
				->field('menu_id,name,icon')->select();
		}
		$company_info = $this->alias('a')->join('left JOIN vrnews1.company c ON c.company_id=a.company_id')->where(['a.admin_id'=>$admin_id])->find();
		//新闻业务
		foreach($column_role_list as $column_role_key=>$column_role_value){
			$Administrator_arr['column_role'][] = $column_role_value;
		}
		//其他业务
		foreach($sys_menu_role_list as $sys_menu_role_key=>$sys_menu_role_value){
			$Administrator_arr['sys_menu_role'][] = $sys_menu_role_value;
		}
		//地区
		if(isset($area_role_list)){
			foreach($area_role_list as $area_role_key=>$area_role_value){
				$Administrator_arr['area_role'][] = $area_role_value;
			}
		}
		//公司
        if(isset($company_info)){
//		    foreach ($company_info as $key=>$val){
//                $Administrator_arr['company_info'] = $val;
//            }
			$Administrator_arr['company_info'] = $company_info['name'];
        }

		$Administrator_arr['column_role_count'] = count($column_role_list);
		$Administrator_arr['sys_menu_role_count'] = count($sys_menu_role_list);
		return $Administrator_arr;
	}
	
	
	//业务管理员删除
	function adminUserDel($admin_id = null){
		if($admin_id){
			$this->startTrans();
			$flag1=false;
			$flag2=false;
			$data['status'] = 0;
			$hid = $this->where('admin_id = '.$admin_id)->data($data)->save();
			if($hid){
				//删除此业务管理的新闻业务管理权限
				$news_column_model = M("vrnews1.column_admin",'news_');
				$news_column_count = $news_column_model->where('admin_id = '.$admin_id)->count();
				if($news_column_count > 0){
					if($news_column_model->where('admin_id = '.$admin_id)->delete()) $flag1 = true;
				}else{
					$flag1 = true;
				}
				
				//删除此业务管理的其他业务管理权限
				$sys_menu_model = M("vradmin1.menu_admin",'sys_');
				$sys_menu_count = $sys_menu_model->where('admin_id = '.$admin_id)->count();
				if($sys_menu_count > 0){
					if($sys_menu_model->where('admin_id = '.$admin_id)->delete()) $flag2 = true;
				}else{
					$flag2 = true;
				}
			}
			if(!$flag1 || !$flag2){
				$this->rollback();
			}else{
				$this->commit();
				return true;
			}
		}
		return false;
	}
	
	
}

